#include "headfiles.h"

using namespace std;

string minSubStrWindow(string str, string sub_str) {
    int char_count[256] = {0};
    int diff_char_num = 0;
    int i;
    for (i = 0; i < sub_str.size(); i++) {
        if (char_count[sub_str[i]] == 0) {
            diff_char_num++;
        }
        char_count[sub_str[i]]++;
    }

    int chars[256] = {0};
    int j, start, min_win = str.size() + 1;
    int find_char_count = 0;
    j = 0;
    for (i = 0; i < str.size(); i++) {
        if (char_count[str[i]] > 0) {
            chars[str[i]]++;
            if (chars[str[i]] == char_count[str[i]]) {
                find_char_count++;
            }

            if (find_char_count == diff_char_num) {
                while (j <= i) {
                    if (char_count[str[j]] > 0) {
                        if (chars[str[j]] > char_count[str[j]]) {
                            chars[str[j]]--;
                        }
                        else {
                            break;
                        }
                    }
                    j++;
                }

                if (min_win > i - j + 1) {
                    start = j;
                    min_win = i - j + 1;
                }
            }
        }
    }

    if (min_win == str.size() + 1) {
        return string();
    }

    return str.substr(start, min_win);
}

